﻿using System;
using System.Xml;
using System.Text;
using Microsoft.Practices.RecipeFramework;
using Microsoft.Practices.RecipeFramework.Library;
using Conf = Microsoft.Practices.RecipeFramework.Configuration;
using Microsoft.Practices.RecipeFramework.Services;
using System.ComponentModel.Design;
using EnvDTE;
using ProjectBase.Guidance.Helpers;
using Microsoft.Practices.Common;
using System.Collections.Specialized;

namespace ProjectBase.Guidance.ValueProviders
{
    public class BoolValueProvider: ValueProvider, IAttributesConfigurable
    {
        /// <summary>
        /// Constants for handling string in xml recipes
        /// </summary>
        class Constants
        {
            Constants() { }
            public const string Expression = "Expression";
        }

        #region IAttributesConfigurable Members

        void IAttributesConfigurable.Configure(StringDictionary attributes)
        {
            if (!attributes.ContainsKey(Constants.Expression))
            {
                throw new ArgumentException("Expression argument in attributes is mandatory");
            }
        }

        #endregion

        public override bool OnBeginRecipe(object currentValue, out object newValue)
        {
            if (currentValue != null)
            {
                newValue = null;

                return false;
            }
            else
            {
                DTE dte = GetService<DTE>(true);

                IConfigurationService cs = GetService<IConfigurationService>(true);

                Conf.Argument argument = cs.CurrentRecipe.ArgumentsByName[Argument.Name];

                switch (Utils.AttributeByName(argument.ValueProvider.AnyAttr, Constants.Expression))
                {
                    case "True":
                        newValue = true;
                        break;
                    case "true":
                        newValue = true;
                        break;
                    case "1":
                        newValue = true;
                        break;
                    default:
                        newValue = false;
                        break;
                }
                return true;
            }
        }
    }
}
